<?php

namespace app\common\model;

use Exception;
use think\Model;
use XznsTools\cache\CacheManage;

/**
 * 投诉举报表
 * Class Informant
 * @package app\common\model
 */
class Informant extends BaseCache
{
    protected $table = 'wechatadmin.system_informant';// 定义表名
    protected $pk = 'id';// 定义主键名
    protected $cacheModel = 'content.complain';

    /**
     * 获取举报内容列表
     * @param $type 1=》文章评论举报 ， 2=》内容投诉
     * @return array
     * @throws Exception
     */
    public function getInformantListFromCache(int $type)
    {
        $ids = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $type)
            ->ZsGetIocData('list', function () use ($type)
            {
                return $this->getInformantListFromDms($type);
            });

        $data = [];
        if ($ids)
        {
            $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel)
                ->batchGetCache($ids, function ($ids)
                {
                    return $this->getInfoByIdsInData($ids);
                }, [], 'list_details');
            array_multisort(array_column($data, 'id'), SORT_NUMERIC, SORT_ASC, $data);
        }
        return $data;
    }

    /**
     * 数据库获取举报所有id
     * @param int $type
     * @return array
     */
    public function getInformantListFromDms(int $type)
    {
        try
        {
            $ids = $this->where(['status' => 1, 'type' => $type])
                ->order('id', 'DESC')
                ->column('id', 'id');

            $ids = array_flip_key_value($ids);
        }
        catch (Exception $e)
        {
            $ids = [];
        }

        return $ids;
    }

    /**
     * 数据库批量获取数据
     * @param $ids
     * @return array
     */
    public function getInfoByIdsInData($ids, $type = 0, $sortType = 'DESC')
    {
        $ids = is_array($ids) ? $ids : explode(',', $ids);
        try
        {
            $data = $this->where('id', 'in', $ids)->column('id,type,status,content', 'id');
        }
        catch (Exception $e)
        {
            $data = [];
        }

        return $data;
    }
}